<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 11/14/18
 * Time: 4:18 PM
 */

namespace caroltc\jenkinsapp\controller;


use caroltc\jenkinsapp\library\DbHandler;
use caroltc\jenkinsapp\library\JenkinsSDK;

class JenkinsConfig extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->checkUserSession();
    }

    /**
     * 获取dingtalk配置列表
     * @return null
     */
    public function getConfigList()
    {
        try {
            return $this->responseOk(['result' => (new DbHandler())->getJenkinsConfigList($this->user_id)]);
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }

    }

    /**
     * 添加配置
     * @return null
     */
    public function addConfig()
    {
        $this->checkParams(['jenkins_name', 'jenkins_url', 'jenkins_username']);
        $jenkins_password = $this->getRequestParam('jenkins_password', '', false);
        $jenkins_api_token = $this->getRequestParam('jenkins_api_token', '', false);
        if (empty($jenkins_api_token) && empty($jenkins_password)) {
            return $this->responseFail(40000, 'jenkins_password,jenkins_api_token不能同时为空');
        }
        $new_data = [
            'jenkins_name' => $this->getRequestParam('jenkins_name'),
            'jenkins_url' => $this->getRequestParam('jenkins_url'),
            'jenkins_username' => $this->getRequestParam('jenkins_username'),
            'jenkins_password' => $jenkins_password,
            'jenkins_api_token' => $jenkins_api_token
        ];
        try {
            $new_data['jenkins_version'] = (new JenkinsSDK($new_data['jenkins_url'], $new_data['jenkins_username'],
                $new_data['jenkins_password'], $new_data['jenkins_api_token']))->getJenkinsVersion();
            $DbHandler = new DbHandler();
            $DbHandler->addJenkinsConfig($this->user_id, $new_data);
            return $this->responseOk(['result' => $DbHandler->getJenkinsConfigList($this->user_id)]);
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }

    }

    /**
     * 编辑配置
     * @return null
     */
    public function editConfig()
    {
        $this->checkParams(['jenkins_config_id', 'jenkins_name', 'jenkins_url', 'jenkins_username']);
        $jenkins_password = $this->getRequestParam('jenkins_password', '', false);
        $jenkins_api_token = $this->getRequestParam('jenkins_api_token', '', false);
        if (empty($jenkins_api_token) && empty($jenkins_password)) {
            return $this->responseFail(40000, 'jenkins_password,jenkins_api_token不能同时为空');
        }
        try {
            $DbHandler = new DbHandler();
            $jenkins_config_id = $this->getRequestParam('jenkins_config_id');
            $config = $DbHandler->getJenkinsConfigByUserIdAndConfigId($this->user_id, $jenkins_config_id);
            if (empty($config)) {
                throw new \Exception('Config not Exist');
            }
            $update_data = [
                'jenkins_name' => $this->getRequestParam('jenkins_name'),
                'jenkins_url' => $this->getRequestParam('jenkins_url'),
                'jenkins_username' => $this->getRequestParam('jenkins_username'),
                'jenkins_password' => $jenkins_password,
                'jenkins_api_token' => $jenkins_api_token
            ];
            $update_data['jenkins_version'] = (new JenkinsSDK($update_data['jenkins_url'], $update_data['jenkins_username'],
                $update_data['jenkins_password'], $update_data['jenkins_api_token']))->getJenkinsVersion();
            $DbHandler->editJenkinsConfig($jenkins_config_id, $this->user_id, $update_data);
            return $this->responseOk();
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }

    }

    /**
     * 删除配置
     * @return null
     */
    public function deleteConfig()
    {
        $this->checkParams(['jenkins_config_id']);
        try {
            $DbHandler = new DbHandler();
            $jenkins_config_id = $this->getRequestParam('jenkins_config_id');
            $config = $DbHandler->getJenkinsConfigByUserIdAndConfigId($this->user_id, $jenkins_config_id);
            if (empty($config)) {
                throw new \Exception('Config not Exist');
            }
            $DbHandler->deleteJenkinsConfig($jenkins_config_id, $this->user_id);
            return $this->responseOk();
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }

    }
}